<?php
namespace app\admin\controller;
use app\admin\model\myshuju\MyHandle;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Mysystems extends BaseController
{
    public function show_list(Request $request){
        $obj = MyHandle::getForm();
        $obj->setModelId(13);
//        $field = $obj->field;
        $table = Builder::table();
        return $table->setAction($request->controller().'/page')
            ->setPage(true)
            ->addToolbarDemo('alldel','全部删除',[],$request->controller().'/del')
            ->addToolbarDemo('add','添加',[],$request->controller().'/show_add')
            ->setMonitorAction('switch',$request->controller().'/change')
            ->setRightHeader('操作')
            ->setButton('del','删除',$request->controller().'/del')
            ->setButton('edit','修改',$request->controller().'/show_edit')
            ->setColumn('input','en_name','配置所属')
            ->setColumn('input','name','配置名称')
            ->setColumn('input','descs','描述')
            ->setColumn('input','tupian','类型')
            ->setColumn('input','sort','排序')
            ->setColumn('switch','delete','是否可删除')
            ->setColumn('switch','status','状态')
            ->fetch();
    }
    public function page(Request $request){
        $data = Db::name(getTable('systems'))
            ->order('en_name desc,sort desc')
            ->paginate(10)
            ->toArray();
        return json_page_response($data);
    }
    public function show_add(Request $request){
        return Builder::form()
            ->setAction($request->controller().'/add')
            ->setProject('input','name','配置名称')
            ->setProject('input','descs','描述')
            ->setProject('input','en_name','配置所属')
            ->setProject('input','sort','排序',100)
            ->setProject('select','tupian','类型',null,[
                '单行文本',
                '多行文本',
                '单文件',
                '多文件',
                '单图片',
                '多图片',
                '视频',
                '单选框',
                '多选框',
                '下拉框',
                '编辑框',
                '编辑框(layui)',
                '开关',
                '时间',
                'json数组'
            ])
            ->setProject('input','option','选项(多个以英文,分割)')
            ->setProject('switch','delete','是否可删除')
            ->setProject('switch','status','是否启用')
            ->setButton('submit','提交')
            ->setButton('back','返回')
            ->fetch();
    }
    public function add(Request $request){
        $data = $request->param();
        if($data['/admin/mysystems/add_html']??null){
            unset($data['/admin/mysystems/add_html']);
        }
        if($data['delete']??null){
            $data['delete'] = 1;
        }else{
            $data['delete'] = 0;
        }
        if($data['status']??null){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $obj = MyHandle::GetConfig();
        $data = $obj->guolu($data,['/admin/mysystems/add_html']);
        if(Db::name(getTable('systems'))->strict(false)->insert($data)){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }

    public function show_edit(Request $request){
        $data = $request->param();
        $shuju = Db::name(getTable('systems'))
            ->where('id',$data['id'])
            ->find();
        return Builder::form()
            ->setAction($request->controller().'/edit')
            ->setProject('none','id','',$shuju['id'])
            ->setProject('input','name','配置名称',$shuju['name'])
            ->setProject('input','descs','描述',$shuju['descs'])
            ->setProject('input','en_name','配置所属',$shuju['en_name'])
            ->setProject('input','sort','排序',$shuju['sort'])
            ->setProject('select','tupian','类型',$shuju['tupian'],[
                '单行文本',
                '多行文本',
                '单文件',
                '多文件',
                '单图片',
                '多图片',
                '视频',
                '单选框',
                '多选框',
                '下拉框',
                '编辑框',
                '编辑框(layui)',
                '开关',
                '时间',
                'json数组'
            ])
            ->setProject('input','option','选项(多个以英文,分割)',$shuju['option'])
            ->setProject('switch','delete','是否可删除',$shuju['delete'])
            ->setProject('switch','status','是否启用',$shuju['status'])
            ->setButton('submit','提交')
            ->setButton('back','返回')
            ->fetch();
    }
    public function edit(Request $request){
        $data = $request->param();
        $obj = MyHandle::GetConfig();
        $data = $obj->guolu($data,['/admin/mysystems/edit_html']);
        if($data['delete']??null){
            $data['delete'] = 1;
        }else{
            $data['delete'] = 0;
        }
        if($data['status']??null){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(Db::name(getTable('systems'))->where('id',$data['id'])->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function change(Request $request){
        $field = $request->param('field');
        $id = $request->param('id');
        $data = Db::name(getTable('systems'))
            ->where('id',$id)
            ->find();
        if($data[$field]==1){
            $data[$field] = 0;
        }else{
            $data[$field] = 1;
        }
        if(Db::name(getTable('systems'))->where('id',$id)->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function del(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('systems'))
            ->where('id','in',$id)
            ->where('delete',1)
            ->select();
        if(!$data){
            return Index::error('有不可删除数据');
        }
        if(Db::name(getTable('systems'))->where('id','in',$id)->where('delete',1)->delete()){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}